package com.spdb.oa.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import com.spdb.oa.OAService;
import com.spdb.oa.OAServiceImpl;
import com.spdb.oa.OAXMLParser;
import com.spdb.oa.exception.OAException;
import com.spdb.oa.type.Record;
import com.spdb.oa.type.User;
import com.spdb.oa.type.UserListCount;

public class OAUserUtil {
	public static Map<User, UserListCount> selectListCount(String userType, Map<User, UserListCount> map) {
		Map<User, UserListCount> selectedMap = new HashMap<User, UserListCount>();
		Iterator<Map.Entry<User, UserListCount>> iter = map.entrySet().iterator();
		
		while (iter.hasNext()) {
			Map.Entry<User, UserListCount> entry = iter.next();
			if (entry.getKey().userType.equals(userType)) {
				
				selectedMap.put(entry.getKey(), entry.getValue());
				
			}
		}
		
		return selectedMap;
	}
	
	public static Map<User, List<Record>> selectList(String userType, Map<User, List<Record>> map) {
		Iterator<Map.Entry<User,  List<Record>>> iter = map.entrySet().iterator();
		Map<User, List<Record>> userMap = new HashMap<User, List<Record>>(); 
		while (iter.hasNext()) {
			Map.Entry<User,  List<Record>> entry = iter.next();
			if (entry.getKey().userType.equals(userType)) {
				userMap.put(entry.getKey(), entry.getValue());
				
			}
		}
		
		return userMap;		
	}
	
	public static Map<User, UserListCount> selectSelfListCount(Map<User, UserListCount> map) {
		return OAUserUtil.selectListCount(User.SELF, map);
	}
	
	public static Map<User, List<Record>> selectSelfList(Map<User, List<Record>> map) {
		return OAUserUtil.selectList(User.SELF, map);
	}
	
	public static void main(String[] args) throws XPathExpressionException, IOException, SAXException, ParserConfigurationException, OAException {
		OAService service = new OAServiceImpl();
		
		Map<User, UserListCount> map = service.getListCount();
		Map<User, UserListCount> ulc = OAUserUtil.selectSelfListCount(map);
		System.out.println(ulc);
		
		Map<User, List<Record>> mapList = service.getDBList(10, 1);
		
		Map<User, List<Record>> records = OAUserUtil.selectSelfList(mapList);
		System.out.println(records);
	}
}
